package cn.chinaflame.pagodas.web;

import cn.chinaflame.pagodas.Result.Result;
import cn.chinaflame.pagodas.annotation.ApiDoc;
import cn.chinaflame.pagodas.dto.PaOrderDto;
import cn.chinaflame.pagodas.enums.CodeEnum;
import cn.chinaflame.pagodas.req.CallbackPaydataReq;
import cn.chinaflame.pagodas.service.PaMemberService;
import cn.chinaflame.pagodas.service.PaOrderService;
import cn.chinaflame.pagodas.utils.MemberBalance;
import cn.chinaflame.pagodas.utils.ParamPreconditions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 支付回调相关接口
 *
 * @author boming
 * @date 2018年6月21日 下午2:42:32
 */
@RestController
@Transactional(rollbackFor = Exception.class)
@RequestMapping(value = "/openapi/v1/pagodas/callback/")
public class OpenCallbackAPI {

    private static final Logger log = LoggerFactory.getLogger(OpenCallbackAPI.class);
    private static String LightSearchAndSend = "http://192.168.1.35:8006/openapi/v1/pagodas/wrmnl/lightSearchAndSend";
    //	private static String installSearch="http://192.168.1.35:8080/pagodas-1.0.0/openapi/v1/pagodas/wrmnl/lightSend";
    private static String productLightClear = "http://192.168.1.35:8006/openapi/v1/pagodas/wrmnl/lightClear";
    private static String LightSearchAndSend2 = "http://192.168.1.73:8006/openapi/v1/pagodas/wrmnl/lightSearchAndSend";
    private static String productLightClear2 = "http://192.168.1.73:8006/openapi/v1/pagodas/wrmnl/lightClear";

    @Autowired
    private PaOrderService paOrderService;
    @Autowired
    private PaMemberService paMemberService;
    //----------------------回调-----------------------------------------------

    /**
     * 支付回调接口
     *
     * @param merchantId
     * @param version
     * @param paymentOrderId
     * @param orderNo
     * @param orderDatetime
     * @param orderAmount
     * @param payDatetime
     * @param payAmount
     * @param payResult
     * @param returnDatetime
     * @return
     */
    @PostMapping(value = "place_order")
    @ApiDoc(String.class)
    public Object placeOrder(String merchantId, String version, String paymentOrderId, String orderNo, String orderDatetime,
                             String orderAmount, String payDatetime, String payAmount, String payResult, String returnDatetime) {
        CallbackPaydataReq req = new CallbackPaydataReq();
        req.setMerchantId(merchantId);
        req.setVersion(version);
        req.setPaymentOrderId(paymentOrderId);
        req.setOrderNo(orderNo);
        req.setOrderDatetime(orderDatetime);
        req.setOrderAmount(orderAmount);
        req.setPayDatetime(payDatetime);
        req.setPayAmount(payAmount);
        req.setPayResult(payResult);
        req.setReturnDatetime(returnDatetime);
        log.info("支付回调接口req:{}", req);
        PaOrderDto pd = paOrderService.orderUpdate(req);
        ParamPreconditions.checkNotNull(pd, CodeEnum.FAIL.getCode(), "未找到订单信息");

        //更改会员消费金额和最后消费时间
        MemberBalance.memberBalance(paMemberService, pd.getMemberId(), payAmount, payDatetime);
        //---------------------------------------------------------------------------

        return "SUCCESS";
    }

    /**
     * 支付回调接口（废弃）
     *
     * @param merchantId
     * @param version
     * @param paymentOrderId
     * @param orderNo
     * @param orderDatetime
     * @param orderAmount
     * @param payDatetime
     * @param payAmount
     * @param payResult
     * @param returnDatetime
     * @return
     */
    @PostMapping(value = "place_order11")
    @ApiDoc(Result.class)
    public Object placeOrder11(String merchantId, String version, String paymentOrderId, String orderNo, String orderDatetime,
                               String orderAmount, String payDatetime, String payAmount, String payResult, String returnDatetime) {
        log.info("支付回调接口merchantId:{}", merchantId);
        /*paOrderService.orderUpdate(req);*/
        return Result.create(merchantId);
    }

}
